//
//  HHLunBo.m
//  HH快医轮播
//
//  Created by 虎虎 on 16/5/22.
//  Copyright © 2016年 SKY. All rights reserved.
//

#import "HHLunBo.h"
#import <Masonry/Masonry.h>
#import <SVProgressHUD/SVProgressHUD.h>
#import "TAPageControl.h"
#import "NQCharityActivity.h"

@interface HHLunBo ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) MCPagerView *pagerView;
@property (nonatomic, strong) NSTimer *timer;


@end

const static NSInteger imageCount = 4;

@implementation HHLunBo

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self addSubview:self.scrollView];
        [self addSubview:self.pagerView];
        [self setupUi];
        [self initScrollViewimage];
        [self addTimer];
        
    }
    return self;
}
- (void)setupUi {
//    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(self);
//        make.top.equalTo(self);
//        make.bottom.equalTo(self);
//        self.pageControl.currentPage = 0;
//    }];
    self.scrollView.frame = CGRectMake(0, 0, ScrollWidth, ScrollHeight);

    
    [self.pagerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(120);
        make.width.equalTo(@10);
        
        make.centerY.equalTo(self);
//        make.centerX.equalTo(self.mas_right).offset(-16);
//        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(200);
        make.height.equalTo(@10);
        
//        make.bottom.equalTo(self.scrollView).offset(-20);
        //        self.pageControl.currentPage = 0;
//        NSLog(@"%zd",self.pageControl.currentPage);
    }];
}





#pragma mark - 代码实现轮播
- (void)initScrollViewimage {
    //将第最后一张图片放到第一个位置
//    self.scrollView.userInteractionEnabled = YES;
    UIImageView *firstImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"14.png"]];
    firstImageView.frame = CGRectMake(0, 0, ScrollWidth, ScrollHeight);
    [self.scrollView addSubview:firstImageView];
    
    //将第一张图片放到最后一个位置上
    UIImageView *lastImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"11.png"]];
    
    lastImageView.frame = CGRectMake((imageCount + 1) * ScrollWidth, 0, ScrollWidth, ScrollHeight);
    [self.scrollView addSubview:lastImageView];
    //制作第二个位置到倒数第二个位置的图片
    for (NSInteger i = 0; i < imageCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%zd.png",i + 11]]];
        imageView.userInteractionEnabled = YES;
        //1、手指点击事件
        //单指单击
        UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerIncident:)];
        //手指数
        singleFingerOne.numberOfTouchesRequired = 1;
        //点击次数
        singleFingerOne.numberOfTapsRequired = 1;
        //设置代理方法
        singleFingerOne.delegate= self;
        //增加事件者响应者，
        [imageView addGestureRecognizer:singleFingerOne];
        
        //单指双击
        UITapGestureRecognizer *singleFingerTwo =[[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                         action:@selector(fingerIncident:)];
        singleFingerTwo.numberOfTouchesRequired = 1;
        singleFingerTwo.numberOfTapsRequired = 2;
        singleFingerTwo.delegate= self;
        [imageView addGestureRecognizer:singleFingerTwo];
        
        
        //双指单击
        UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                          action:@selector(fingerIncident:)];
        doubleFingerOne.numberOfTouchesRequired = 2;
        doubleFingerOne.numberOfTapsRequired = 1;
        doubleFingerOne.delegate= self;
        [imageView addGestureRecognizer:doubleFingerOne];
        
        //双指双击
        UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                          action:@selector(fingerIncident:)];
        doubleFingerTwo.numberOfTouchesRequired = 2;
        doubleFingerTwo.numberOfTapsRequired = 2;
        doubleFingerTwo.delegate= self;
        [imageView addGestureRecognizer:doubleFingerTwo];
        
        //如果不加下面的话，当单指双击时，会先调用单指单击中的处理，再调用单指双击中的处理
        [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
        //同理双指也是如此
        [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
        
        
        
        
        imageView.frame = CGRectMake(ScrollWidth * (i + 1), 0, ScrollWidth, ScrollHeight);
        
        
        [self.scrollView addSubview:imageView];
    }
    //一开始显示第二个位置的图片
    self.scrollView.contentOffset = CGPointMake(ScrollWidth, 0);
    self.scrollView.contentSize = CGSizeMake((imageCount + 2) * ScrollWidth, 0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    
    
    

    

}

- (void)fingerIncident:(UITapGestureRecognizer *)sender
{
    if (sender.numberOfTouchesRequired==1) {
        //单指点击事件
        if(sender.numberOfTapsRequired == 1) {
            //单指单击
            NSLog(@"单指单击");
            [SVProgressHUD showSuccessWithStatus:@"跳转到公益活动"];
           
            if ([self.delegate respondsToSelector:@selector(HHLunBoJumpToCharity)]) {
                [self.delegate HHLunBoJumpToCharity];
            }
        }
        else if(sender.numberOfTapsRequired ==2 ){
            //单指双击
            NSLog(@"单指双击");
            if ([self.delegate respondsToSelector:@selector(HHLunBoJumpToCharity)]) {
                [self.delegate HHLunBoJumpToCharity];
            }
        }
    }
    else if (sender.numberOfTouchesRequired==2) {
        //双指点击事件
        if(sender.numberOfTapsRequired == 1) {
            //双指单击
            NSLog(@"双指单击");
            if ([self.delegate respondsToSelector:@selector(HHLunBoJumpToCharity)]) {
                [self.delegate HHLunBoJumpToCharity];
            }
        }
        else if(sender.numberOfTapsRequired ==2 ){
            //双指双击
            NSLog(@"双指双击");
            if ([self.delegate respondsToSelector:@selector(HHLunBoJumpToCharity)]) {
                [self.delegate HHLunBoJumpToCharity];
            }
        }
    }
}



- (void)addTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

- (void)nextPage {
    CGFloat scrollWidth = self.scrollView.frame.size.width;
    NSInteger index = self.pagerView.page;

//    NSLog(@"%zd",index);
    if (index == imageCount + 1) {
        index = 0;
    } else {
        index ++;
    }
    [self.scrollView setContentOffset:CGPointMake((index + 1) * scrollWidth, 0) animated:YES];
    
}

- (void)scrollviewFinish:(UIScrollView *)scrollView {
    CGFloat scrollWidth = self.scrollView.frame.size.width;
    NSInteger index = (self.scrollView.contentOffset.x + scrollWidth * 0.5) / scrollWidth;
    if (index == imageCount + 1) {
        //当显示到最后一个位置的时候,设置为第二个位置的图片,也就是第一张图
        [self.scrollView setContentOffset:CGPointMake(scrollWidth, 0) animated:NO];
    }else if (index == 0) {
        [self.scrollView setContentOffset:CGPointMake(imageCount * scrollWidth, 0) animated:NO];
    }
}


#pragma mark - scrollView代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scrollWidth = self.scrollView.frame.size.width;
    NSInteger index = (self.scrollView.contentOffset.x + scrollWidth * 0.5) / scrollWidth;
    if (index == imageCount + 2) {
        index = 1;
    } else if (index == 0) {
        index = imageCount;
    }
    self.pagerView.page = index - 1;
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.timer invalidate];
    self.timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    [self addTimer];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    //NSLog(@"滚动结束");
    //此方法，
    [self scrollviewFinish:scrollView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //NSLog(@"减速至滚动停止");
    [self scrollviewFinish:scrollView];
}

#pragma mark - 懒加载
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
                _scrollView.delegate = self;
    }

    return _scrollView;
}

- (MCPagerView *)pagerView {
    if (_pagerView == nil) {
        _pagerView = [[MCPagerView alloc] init];
        
//        _pageControl.numberOfPages = imageCount;
//        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
//        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
//        _pageControl.currentPage = 0;
        _pagerView.pattern = @"aaaa";
//        _pagerView.backgroundColor = [UIColor whiteColor];
        [_pagerView setImage:[UIImage imageNamed:@"a-h.png"] highlightedImage:[UIImage imageNamed:@"a.png"] forKey:@"a"];
        
//        [_pagerView setImage:[UIImage imageNamed:@"c.png"] highlightedImage:[UIImage imageNamed:@"c-h.png"] forKey:@"c"];
        
    }
    return _pagerView;
}


@end
